/**
 * 
 */
package routing;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.jgrapht.GraphPath;
import org.jgrapht.Graphs;
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import routing.route.Route;
import routing.table.RouteTable;

/**
 * @author chevy
 *
 */
public class OSPFAlgorithm implements RoutingProtocol {

	public RouteTable createTable(String currentRouter,
			SimpleWeightedGraph<String, DefaultEdge> topology) {
		
			RouteTable routingTable = new RouteTable();
			
			Set<String> vertices = topology.vertexSet();
			
			
			for(String vertex : vertices ){
				
				if(vertex.equals(currentRouter)){
					continue;
				}
				
				DijkstraShortestPath<String, DefaultEdge> dijkstra = 
						new DijkstraShortestPath<String, DefaultEdge>(topology, currentRouter, vertex);
				
				GraphPath<String, DefaultEdge> path = dijkstra.getPath();
				
				List<String> vertexList = Graphs.getPathVertexList(path);
				
				System.out.println("Creará una ruta en " + currentRouter + " al nodo " + vertex + " por channel" + vertexList.get(1));
				
				HashMap<String, Double> pos = new HashMap<String, Double>();
				
				pos.put("channel"+vertexList.get(1), (double) 1);
				
				Route route = new Route(vertex, pos, 1);
				
				routingTable.evaluateRoute(route);
				
			}
			
		return routingTable;
	}

	public String getDestinationRoute(Route route) {
		Set<String> o = route.getPosibleExitInterfaces().keySet();
		Iterator<String> it = o.iterator();
		return it.next();
	}

	

}
